/*
*  This file is part of mangusta
*
*  mangusta is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  mangusta is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with mangusta; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
*
*  Author: <antonino.calderone@ericsson.com>, <acaldmail@gmail.com>
*
*/


/* -------------------------------------------------------------------------- */

#include "nu_rt_prog_ctx.h"
#include "nu_stmt_end_func.h"
#include "nu_error_codes.h"
#include "nu_lang_defs.h"


/* -------------------------------------------------------------------------- */

namespace nu
{

/* -------------------------------------------------------------------------- */

stmt_end_func_t::stmt_end_func_t(prog_ctx_t & ctx)
   : stmt_t(ctx)
{
   syntax_error_if(ctx.compiletime_pc.get_stmt_pos() > 0,
                   "} for \"func\" statement must be a first statement of the line");

   auto handle = ctx.procedure_metadata.end_find(ctx.compiletime_pc);

   if (!handle)
      handle = ctx.procedure_metadata.compile_end(ctx.compiletime_pc);

   if (handle)
      handle->pc_end_stmt = ctx.compiletime_pc;

   ctx.parsing_scope.pop();
   ctx.parsing_func.pop();
}


/* -------------------------------------------------------------------------- */

void stmt_end_func_t::run(rt_prog_ctx_t & ctx)
{
   auto rt_error_if = [&](
                         bool cond,
                         rt_error_code_t::value_t err,
                         const std::string desc)
   {
      rt_error_code_t::get_instance().throw_if(
         cond,
         ctx.runtime_pc.get_line(),
         err,
         desc);
   };

   auto handle = ctx.procedure_metadata.end_find(ctx.runtime_pc);

   rt_error_if(!handle || handle->identifier.empty(),
               rt_error_code_t::E_NO_MATCH_FUNC, "");

   if (! handle->flag[instrblock_t::EXIT])
   {
      ctx.flag[rt_prog_ctx_t::FLG_RETURN_REQUEST] = true;

      // Retrieve name of this this function
      const std::string& identifier = handle->identifier;

      auto scope_type = ctx.proc_scope.get_type(identifier);

      if (identifier != PROGLANG_MAIN_FUNCTION)
      {
         //The return-value (same function name) must be defined
         rt_error_if(scope_type != proc_scope_t::type_t::LOCAL,
                     rt_error_code_t::E_NO_RET_VAL,
                     " '" + identifier + "' not defined. ");

         bool expected_retval = ctx.proc_scope.is_func_call(identifier);

         if (expected_retval)
         {
            // Get return-value
            variant_t value = (*(ctx.proc_scope.get()))[identifier];

            // Insert the return value in the context
            ctx.function_retval_tbl[identifier].push_back(value);
         }

         // Leave the function scope
         ctx.proc_scope.exit_scope();
      }

      else
      {
         //TODO prog ret val here
         ctx.flag[rt_prog_ctx_t::FLG_END_REQUEST] = true;
      }
   }

   else
   {
      //SUB completed, go to next line
      ctx.go_to_next();
   }

}


/* -------------------------------------------------------------------------- */

stmt_t::stmt_cl_t stmt_end_func_t::get_cl() const throw()
{
   return stmt_cl_t::FUNC_END;
}


/* -------------------------------------------------------------------------- */

}


/* -------------------------------------------------------------------------- */

